package com.sunday.aomicaichang.adpter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.base.BaseApp;
import com.sunday.aomicaichang.ui.MenuDetailActivity;
import com.sunday.aomicaichang.ui.common.WebViewActivity;
import com.sunday.aomicaichang.ui.product.ProductActivity;
import com.sunday.member.entity.product.CollectionProduct;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/4.
 */

public class CollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<CollectionProduct> collectionProductList;
    private String status;
    private View.OnClickListener onClickListener;

    public CollectionAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setmData(List<CollectionProduct> collectionProductList, String status) {
        this.collectionProductList = collectionProductList;
        this.status = status;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if ("1".equals(status)) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collection_goods, parent, false);
            return new ItemViewHolder1(v);
        } else if ("2".equals(status)) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collection_menu, parent, false);
            return new ItemViewHolder2(v);
        } else {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collection_data, parent, false);
            return new ItemViewHolder3(v);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final CollectionProduct collectionProduct = collectionProductList.get(position);

        if (collectionProduct == null)
            return;

        if (holder instanceof ItemViewHolder1) {//首页banner
            ItemViewHolder1 viewHolder = (ItemViewHolder1) holder;
            if (!TextUtils.isEmpty(collectionProduct.getImage())){
                Picasso.with(mContext)
                        .load(collectionProduct.getImage())
                        .error(R.mipmap.ic_product)
                        .resize(150,150).into(viewHolder.img);
            }
            if (collectionProduct.getName() != null)
                viewHolder.tvName.setText(collectionProduct.getName());
                viewHolder.tvNum.setText("销量："+collectionProduct.getSaleNumber());
            if (collectionProduct.getMarketPrice() != null) {
                viewHolder.tvOriginalPrice.setText("￥"+collectionProduct.getMarketPrice() + "");
                viewHolder.tvOriginalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            }
            if (collectionProduct.getPrice() != null)
                viewHolder.tvPrice.setText(collectionProduct.getPrice() + "");

            viewHolder.imgDel.setOnClickListener(onClickListener);
            viewHolder.imgDel.setTag(position);
            viewHolder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ProductActivity.class);
                    intent.putExtra("id", collectionProduct.getId());
                    intent.putExtra("name", collectionProduct.getName());
                    mContext.startActivity(intent);
                }
            });
        } else if (holder instanceof ItemViewHolder2) {

            ItemViewHolder2 viewHolder = (ItemViewHolder2) holder;
            if (!TextUtils.isEmpty(collectionProduct.getImage())){
                Picasso.with(mContext)
                        .load(collectionProduct.getImage())
                        .error(R.mipmap.ic_product)
                        .resize(150,150).into(viewHolder.img);
            }
            if (collectionProduct.getName() != null)
                viewHolder.tvName.setText(collectionProduct.getName());
            if (collectionProduct.getName() != null)
                viewHolder.tvSub.setText(collectionProduct.getViceName());
            if (collectionProduct.getCreateDate() != null)
                viewHolder.tvTime.setText(collectionProduct.getCreateDate());
            viewHolder.imgDel.setOnClickListener(onClickListener);
            viewHolder.imgDel.setTag(position);
            viewHolder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, MenuDetailActivity.class);
                    intent.putExtra("id", collectionProduct.getId());
                    mContext.startActivity(intent);
                }
            });
        } else {
            ItemViewHolder3 viewHolder = (ItemViewHolder3) holder;
            if (!TextUtils.isEmpty(collectionProduct.getImage())){
                Picasso.with(mContext)
                        .load(collectionProduct.getImage())
                        .error(R.mipmap.ic_product)
                        .resize(150,150).into(viewHolder.img);
            }
            if (collectionProduct.getName() != null)
                viewHolder.tvSub.setText(collectionProduct.getName());
            if (collectionProduct.getCreateDate() != null)
                viewHolder.tvTime.setText(collectionProduct.getCreateDate());

            viewHolder.imgDel.setOnClickListener(onClickListener);
            viewHolder.imgDel.setTag(position);
            viewHolder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   Intent intent = new Intent(mContext, WebViewActivity.class);

                    long memberId = BaseApp.getInstance().getMember().getId();
                    intent.putExtra("url", collectionProduct.getUrl()+"&memberId="+memberId);
                    intent.putExtra("title", collectionProduct.getName());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
    @Override
    public int getItemCount() {
        return collectionProductList == null ? 0 : collectionProductList.size();
    }


    public class ItemViewHolder3 extends RecyclerView.ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.tv_sub)
        TextView tvSub;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.img_del)
        ImageView imgDel;
        @Bind(R.id.item)
        RelativeLayout item;

        public ItemViewHolder3(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public class ItemViewHolder2 extends RecyclerView.ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sub)
        TextView tvSub;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.img_del)
        ImageView imgDel;
        @Bind(R.id.item)
        RelativeLayout item;

        public ItemViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public class ItemViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_num)
        TextView tvNum;
        @Bind(R.id.tv1)
        TextView tv1;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_original_price)
        TextView tvOriginalPrice;
        @Bind(R.id.img_del)
        ImageView imgDel;
        @Bind(R.id.item)
        RelativeLayout item;

        public ItemViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
